package org.jiucai.cas.auth.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.jiucai.appframework.base.spring.service.BaseService;
import org.jiucai.cas.data.Constant;
import org.jiucai.cas.domain.UserInfo;


public class AuthResultServiceImpl extends BaseService implements AuthResultService {

	
	
	
	@Override
	public void onLoginSuccess(UserInfo user, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession(true);

		// 通过认证后，设置会话用户ID
		session.setAttribute(Constant.AUTH_SESS_UID,
				user.getUserId());
		// 设置用户信息到会话
		session.setAttribute(Constant.AUTH_SESS_USER, user);


		Boolean isForward = false;
		String referer = request.getHeader("Referer");
		
		//单点登录成功后跳转的url地址
		String r = request.getParameter(Constant.REQ_AUTH_URL);
		
		if(StringUtils.isNotBlank(r)){
			log.debug("sso redirect url: " + r);
			response.sendRedirect(r);
			return;
		}
		
		
		log.info("登录成功, Referer: " + referer);
		
		//如果的登录前的页面是本应用，则登录成功后跳转到登录前页面。by zhaidw at 2011-12-14
		if (StringUtils.isNotBlank(referer)) {
			
			String serverPath = request.getContextPath();
			String basePath = new StringBuffer(request.getScheme()).append("://").append(request.getServerName()).append(":").append(request.getServerPort()).append(serverPath).toString();

			//不能再次跳转到登录认证服务地址
			//http://172.16.0.174:8080/adwiser-report/auth/login
			
			String loginAuthUrl  = basePath + "/auth/login";
			String logoutAuthUrl = basePath + "/auth/logout";
			
			if(referer.startsWith(basePath) && !referer.equals(loginAuthUrl) && !referer.equals(logoutAuthUrl) ){
				isForward = false;
				
				response.sendRedirect(referer);
				
			}else{
				isForward = true;
			}
			
		}else{
			isForward = true;
		}

		if(isForward){
			request.getRequestDispatcher(Constant.URL_AUTH_SUCCESS_URL).forward(request, response);
		}

	}

	@Override
	public void onLoginFailed(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String uri = request.getRequestURI();
		// 定向到错误页面
		log.debug("访问 " + uri + " 失败，转发请求至 " + Constant.URL_LOGIN_FAILED);

		request.getRequestDispatcher(Constant.URL_LOGIN_FAILED).forward(request, response);
		

	}

	@Override
	public void onLogoutSuccess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {



		HttpSession session = request.getSession(true);

		log.debug("user logout, remove session");

		//LogService.log(OpType.logout, "注销成功", request);

		clearSession(session);
		// response.sendRedirect(request.getContextPath()
		// + Constant.URL_LOGOUT);

		request.setAttribute("failed_msg", "您已成功退出");

		request.getRequestDispatcher(Constant.URL_LOGOUT).forward(
				request, response);
		
		
	}

	@Override
	public void onLogoutFailed(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("failed_msg", "注销失败，请直接关闭浏览器");
		//request.getRequestDispatcher(Constant.URL_AUTH_FAILED).forward(request, response);
		request.getRequestDispatcher(Constant.URL_LOGIN_FAILED).forward(request, response);
		
	}
	
	
	/**
	 * 清除会话内容
	 * 
	 * @param session
	 */
	protected  void clearSession(HttpSession session) {
		// 会话用户ID
		session.removeAttribute(Constant.AUTH_SESS_UID);
		// 会话用户信息
		session.removeAttribute(Constant.AUTH_SESS_USER);

		session.invalidate();
	}

}
